package com.yj.event_listener.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class RemarkUtil {

    public static String getSenderTitle(Context context, String senderAddress) {
        String title = senderAddress; // Default to sender address if no title found

        // Query the contacts database to get the sender's name or custom label
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(senderAddress));
        Cursor cursor = contentResolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            title = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            cursor.close();
        }

        return title;
    }

    public static String getContactIdFromPhoneNumber(Context context, String phoneNumber) {
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Cursor cursor = context.getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup._ID}, null, null, null);

        String contactId = null;
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup._ID);
            if(columnIndex >= 0){
                contactId = cursor.getString(columnIndex);
            }
            cursor.close();
        }

        return contactId;
    }

    public static String getContactRemarkFromId(Context context, String contactId) {
        if (contactId == null) {
            return null;
        }

        String remark = null;
       /* Cursor cursor = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.CommonDataKinds.Note.NOTE},
                ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
                new String[]{contactId, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE},
                null
        );*/
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID + " = ?",
                new String[]{contactId},
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
//            String[] arr = cursor.getColumnNames();
//            for (String str : arr){
//                LogUtil.d(str);
//                LogUtil.d("targetColumn "+ContactsContract.CommonDataKinds.Note.NOTE);
//            }
            int columnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE);
            if(columnIndex >= 0){
                remark = cursor.getString(columnIndex);
            }
            cursor.close();
        }

        return remark;
    }
}
